<?php
class C_SurgeryEquipment extends Controller {

	var $template_mod;

	function C_SurgeryEquipment($template_mod = "general") {
		parent::Controller();
		if (Celini::getCurrentController() == 'SurgeryEquipment') {
			$menu =& Menu::getInstance();
			$menu->currentSection = 'patient';
		}
		$this->template_mod = $template_mod;
		$this->view->path = 'surgery_equipment';
	}

	public function actionGetTemplate($templateId) {
		$templateId = $_GET['templateId'];

		$tpl = Celini::newORDO('TemplateEquipment');
		$medicineList = $tpl->getMedicines($templateId);
		$equipmentList = $tpl->getEquipments($templateId);

		$result = $this->buildTemplateTable($medicineList, $equipmentList, true);
		$this->assignCommonInfo($result);

		$tpl->setup($templateId);
		$this->assign('templateName', $tpl->get('name'));
		$this->assign('templateDescription', $tpl->get('description'));
		$this->assign('currentTemplateId', $templateId);
		return $this->view->render('equipment_template.html');
	}

	public function actionPickupTemplate() {
		$templateId = $_GET['templateId'];
		$tpl = Celini::newORDO('TemplateEquipment');
		$medicineList = $tpl->getMedicines($templateId);
		$equipmentList = $tpl->getEquipments($templateId);

		$result = $this->buildTemplateTable($medicineList, $equipmentList, false);
		return json_encode($result);
	}

	public function actionGetCurrentEquipment(){
		$medicalRecordId = $_GET['medical_record_id'];
		if(!isset($medicalRecordId) ){
			return;
		}

		$surgery = Celini::newORDO('SurgeryMedicalRecord');
		$medicineList = $surgery->getMedicines($medicalRecordId);
		$equipmentList = $surgery->getEquipments($medicalRecordId);

		$result = $this->buildTemplateTable($medicineList, $equipmentList, false);
		$this->assignCommonInfo($result);

		$templates = Celini::newORDO('TemplateEquipment')->getAllTemplate();
		$this->assign('templates', $templates);
		if (sizeof($templates) > 0) {
			$this->assign('firstTemplateId', $templates[0]['template_equipment_id']);
		}
		return $this->view->render("input_equipment.html");
	}

	public function actionSearchMedicine(){
		$search_key = trim(($_GET['q']));
		$medicine = Celini::newORDO('SurgeryEquipment')->getMedicines($search_key, $_GET['limit']);
		foreach ($medicine as $value) {
			echo "{$value['name']}\n";
		}
	}

	public function actionSearchEquipment(){
		$medicine = Celini::newORDO('SurgeryEquipment')->getEquipments($_GET['q'], $_GET['limit']);
		foreach ($medicine as $value) {
			//@LOCALIZATION
			echo "{$value['name']}\n";
		}
	}

	public function actionGetEquipmentInfAjax(){
		$name = trim($_POST['equipment_name']);
		$medicine = Celini::newORDO('SurgeryEquipment')->getEquipmentInfo($name);
		if(!isset($medicine) || count($medicine) <= 0){
			return 1;
		}
		return json_encode($medicine[0]);
	}

	public function actionSaveEquipmentTemplateAjax() {
		$medicineNum 	= $_GET['template_medicine_table_size'];
		$equipmentNum 	= $_GET['template_equipment_table_size'];
		$templateId 	= $_GET['template_id'];

		$template = Celini::newORDO('TemplateEquipment', $templateId);
		$template->set('name', $_GET['template_name']);
		$template->set('description', $_GET['template_description']);
		$template->persist();

		Celini::newORDO('TemplateEquipmentItem')->deleteAllRecord($templateId);
		for($i = 0; $i < $medicineNum; $i++ ){
			if(isset($_GET['template_medicine_name_'.$i]) &&  $_GET['template_medicine_id_'.$i] != 0 ){
				$tpl = Celini::newORDO('TemplateEquipmentItem');
				$qualityTitle	= 	Sanitize::removeSpaces($_GET['template_medicine_quantity_title_'.$i]);
				$qualityTitle	=	Celini::newORDO('EquipmentUnit')->getL10nKeyByName($qualityTitle);
				$tpl->addRecord( $templateId, $_GET['template_medicine_id_'.$i], $_GET['template_medicine_quantity_'.$i], $qualityTitle);
			}
		}
		for($j = 0; $j < $equipmentNum; $j++ ){
			if(isset($_GET['template_equipment_name_'.$j]) &&  $_GET['template_equipment_id_'.$j] != 0 ){
				$tpl = Celini::newORDO('TemplateEquipmentItem');
				$qualityTitle 	= 	Sanitize::removeSpaces($_GET['template_equipment_quantity_title_'.$j]);
				$qualityTitle	=	Celini::newORDO('EquipmentUnit')->getL10nKeyByName($qualityTitle);
				$tpl->addRecord( $templateId, $_GET['template_equipment_id_'.$j], $_GET['template_equipment_quantity_'.$j], $qualityTitle);
			}
		}
	}

	public function actionSaveEquipmentAjax(){
		$medicineNum = $_GET['medicine_table_size'];
		$equipmentNum = $_GET['equipment_table_size'];
		$recordId = $_GET['medical_record_id'];

		$qualityTitle = '';

		$surgery = Celini::newORDO('SurgeryMedicalRecord');
		$surgery->deleteAllRecord($_GET['medical_record_id']);

		for($i = 0; $i < $medicineNum; $i++ ){
			if(isset($_GET['medicine_name_'.$i]) &&  $_GET['medicine_id_'.$i] != 0 ){
				$surgery = Celini::newORDO('SurgeryMedicalRecord');
				$qualityTitle 	= 	Sanitize::removeSpaces($_GET['medicine_quantity_title_'.$i]);
				$qualityTitle	=	Celini::newORDO('EquipmentUnit')->getL10nKeyByName($qualityTitle);
				$surgery->addRecord( $recordId, $_GET['medicine_id_'.$i], $_GET['medicine_quantity_'.$i], $qualityTitle);
			}
		}
		for($j = 0; $j < $equipmentNum; $j++ ){
			if(isset($_GET['equipment_name_'.$j]) &&  $_GET['equipment_id_'.$j] != 0 ){
				$surgery = Celini::newORDO('SurgeryMedicalRecord');
				$qualityTitle = Sanitize::removeSpaces($_GET['equipment_quantity_title_'.$j]);
				$qualityTitle	=	Celini::newORDO('EquipmentUnit')->getL10nKeyByName($qualityTitle);
				$surgery->addRecord( $recordId, $_GET['equipment_id_'.$j], $_GET['equipment_quantity_'.$j], $qualityTitle);
			}
		}		

		return L10nHelper::_("Saving sucessful.");
	}


	///////////////////// PRIVATE AREA ////////////////////////
	////////////
	////////////

	private function assignCommonInfo($data) {
		$this->assign('medicineBody', $data['medicineBody']);
		$this->assign('equipmentBody', $data['equipmentBody']);
	}

	private function buildTemplateTable($medicineList, $equipmentList, $template) {
		if(!isset($medicineList) || count($medicineList) <= 0){
			$medicineList = array(
					0	=> array( 'medicine_id'	=> '', 'type'=>'', 'name' => '', 'quantity' => '', 'quantity_title'=> ''),
					1	=> array( 'medicine_id'	=> '', 'type'=>'', 'name' => '', 'quantity' => '', 'quantity_title'=> ''),
					2	=> array( 'medicine_id'	=> '', 'type'=>'', 'name' => '', 'quantity' => '', 'quantity_title'=> ''),
					3	=> array( 'medicine_id'	=> '', 'type'=>'', 'name' => '', 'quantity' => '', 'quantity_title'=> ''),
					4	=> array( 'medicine_id'	=> '', 'type'=>'', 'name' => '', 'quantity' => '', 'quantity_title'=> ''),
					);

		}else{
			$emptyRow = array( 'medicine_id'	=> '', 'type'=>'', 'name' => '', 'quantity' => '', 'quantity_title'=> '');
			array_push($medicineList, $emptyRow);
		}

		if(!isset($equipmentList) || count($equipmentList) <= 0){
			$equipmentList = array(
					0	=> array( 'equipment_id'	=> '', 'type'=>'', 'name' => '', 'quantity' => '', 'quantity_title'=> ''),
					1	=> array( 'equipment_id'	=> '', 'type'=>'', 'name' => '', 'quantity' => '', 'quantity_title'=> ''),
					2	=> array( 'equipment_id'	=> '', 'type'=>'', 'name' => '', 'quantity' => '', 'quantity_title'=> ''),
					3	=> array( 'equipment_id'	=> '', 'type'=>'', 'name' => '', 'quantity' => '', 'quantity_title'=> ''),
					4	=> array( 'equipment_id'	=> '', 'type'=>'', 'name' => '', 'quantity' => '', 'quantity_title'=> ''),
					);

		}else{
			$emptyRow = array( 'equipment_id'	=> '', 'type'=>'', 'name' => '', 'quantity' => '', 'quantity_title'=> '');
			array_push($equipmentList, $emptyRow);
		}

		$documentReady = '';

		$result = array();
		$tmp = $this->buildMedicineTable($medicineList, $template);
		$result['medicineBody']			= $tmp['medicineBody'];
		$result['medicineTableSize']	= $tmp['medicineTableSize'];

		$tmp = $this->buildEquipmentTable($equipmentList, $template);
		$result['equipmentBody']		= $tmp['equipmentBody'];
		$result['equipmentTableSize']	= $tmp['equipmentTableSize'];
		return $result;
	}

	private function buildMedicineTable($medicineList,$template) {
		$i = 0;
		$medicineBody = '';
		foreach($medicineList as $medicine){
			if ($template) {
				$medicineBody .=" <tr>
								<input type='hidden' id='template_medicine_id_$i' name='template_medicine_id_$i' value='{$medicine['medicine_id']}'>
								<td class='first'>
									<span id='template_medicine_type_span_$i' name='template_medicine_type_span_$i'>{$medicine['type']}</span>
								</td>
								<td>
									<input class='auto_complete' type='text' id='template_medicine_name_$i' name='template_medicine_name_$i' value='{$medicine['name']}' onblur=\"javascript:getTemplateEquipmentInfo('yes',$i);\">
								</td>
								<td class='last'>
									<input type='text' id='template_medicine_quantity_$i' name='template_medicine_quantity_$i' size='4' value='{$medicine['quantity']}'>&nbsp;
									<input type='text' id='template_medicine_quantity_title_$i' name='template_medicine_quantity_title_$i' size='6' value='{$medicine['quantity_title']}'>
								</td>
							 </tr>" ;
			} else {
				$medicineBody .=" <tr>
								<input type='hidden' id='medicine_id_$i' name='medicine_id_$i' value='{$medicine['medicine_id']}'>
								<td class='first'>
									<span id='medicine_type_span_$i' name='medicine_type_span_$i'>{$medicine['type']}</span>
								</td>
								<td>
									<input class='auto_complete' type='text' id='medicine_name_$i' name='medicine_name_$i' value='{$medicine['name']}' onblur=\"javascript:getEquipmentInfo('yes',$i);\">
								</td>
								<td class='last'>
									<input type='text' id='medicine_quantity_$i' name='medicine_quantity_$i' size='4' value='{$medicine['quantity']}'>&nbsp;
									<input type='text' id='medicine_quantity_title_$i' name='medicine_quantity_title_$i' size='6' value='{$medicine['quantity_title']}'>
								</td>
							 </tr>" ;
			}

			$i++;
		}

		return array(
			'medicineBody' 	=> $medicineBody,

			'medicineTableSize'	=> $i);
	}

	private function buildEquipmentTable($equipmentList,$template) {
		$j = 0;
		$equipmentBody = '';
		foreach($equipmentList as $equipment ){
			if ($template) {
				$equipmentBody .=" <tr>
								<input type='hidden' id='template_equipment_id_$j' name='template_equipment_id_$j' value='{$equipment['equipment_id']}'>
								<td class='first'>
									<span id='template_equipment_type_span_$j' name='template_equipment_type_span_$j'>{$equipment['type']}</span>
								</td>
								<td>
									<input class='auto_complete' type='text' id='template_equipment_name_$j' name='template_equipment_name_$j' value='{$equipment['name']}' onblur=\"javascript:getTemplateEquipmentInfo('no',$j);\">
								</td>
								<td class='last'>
									<input type='text' id='template_equipment_quantity_$j' name='template_equipment_quantity_$j' size='4' value='{$equipment['quantity']}'>&nbsp;
									<input type='text' id='template_equipment_quantity_title_$j' name='template_equipment_quantity_title_$j' size='6' value='{$equipment['quantity_title']}'>
								</td>
							 </tr>" ;
			} else {
				$equipmentBody .=" <tr>
								<input type='hidden' id='equipment_id_$j' name='equipment_id_$j' value='{$equipment['equipment_id']}'>
								<td class='first'>
									<span id='equipment_type_span_$j' name='equipment_type_span_$j'>{$equipment['type']}</span>
								</td>
								<td>
									<input class='auto_complete' type='text' id='equipment_name_$j' name='equipment_name_$j' value='{$equipment['name']}' onblur=\"javascript:getEquipmentInfo('no',$j);\">
								</td>
								<td class='last'>
									<input type='text' id='equipment_quantity_$j' name='equipment_quantity_$j' size='4' value='{$equipment['quantity']}'>&nbsp;
									<input type='text' id='equipment_quantity_title_$j' name='equipment_quantity_title_$j' size='6' value='{$equipment['quantity_title']}'>
								</td>
							 </tr>" ;
			}

			$j++;
		}

		return array(
			'equipmentBody' 		=> $equipmentBody,
			'equipmentTableSize'	=> $j);
	}

}